package com.myBlog.services.login;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myBlog.pojo.User;
import com.myBlog.repository.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;

@Slf4j
@Service
public class CookieLoginService {

    @Autowired
    private UserMapper userMapper;

    public boolean login(String userName,String passwd){
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //前端md5加密后，后端再次md5加密(转大写)，对比数据库
        try {
            lambdaQueryWrapper.eq(User::getUsername,userName).eq(User::getPasswd, DigestUtils.md5DigestAsHex(passwd.getBytes("utf-8")).toUpperCase() );
        } catch (UnsupportedEncodingException e) {
            log.error("不支持utf-8编码格式",e);
            throw new RuntimeException(e);
        }
        User user = userMapper.selectOne(lambdaQueryWrapper);
        if(user!=null){
            return true;
        }

        return false;
    }

}
